﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class Order_ReturnDoc : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check.CheckPermission("400101");
        if (!IsPostBack)
        {
            TextBox2.Attributes.Add("onclick", "SearchVIP('../');");
            //TextBox5.Attributes.Add("onclick", "ulred();");
            //((Button)BUTTONDJLIST1.Controls[4]).Enabled = false;
            //((Button)BUTTONDJLIST1.Controls[6]).Enabled = false;
            BindDropReason();
            BindDropDuty();
            ORDER_OFCODE();
            //BindDropDDcId();
            Bind(false);
        }
        Session["FCode"] = "400102";
    }
    private void BindDropDDcId()
    {
        //List<Model.RETURNDOC> ddcid = new BLL.RETURNDOC().GetAllList();
        //DropDownList2.DataSource = ddcid;
        //DropDownList2.DataTextField = "RDCID";
        //DropDownList2.DataValueField = "RDCID";
        //ListItem item = new ListItem();
        //item.Value = "-1";
        //item.Text = "全部";
        //DropDownList2.Items.Insert(0, item);
    }

    private void ORDER_OFCODE()
    {
        List<Model.ORDERFROM> properties = new BLL.ORDERFROM().GetAllList();
        DropDownList1.DataSource = properties;
        DropDownList1.DataTextField = "OF_NAME";
        DropDownList1.DataValueField = "OF_CODE";
        DropDownList1.DataBind();
        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "-1";
        DropDownList1.Items.Insert(0, li);
    }
    private void BindDropReason()
    {
        List<Model.RETURNTYPE> reasons = new BLL.RETURNTYPE().GetAllList();
        if (reasons != null)
        {
            dropReason.DataSource = reasons;
            dropReason.DataTextField = "RT_NAME";
            dropReason.DataValueField = "RT_CODE";
            dropReason.DataBind();
            ListItem item = new ListItem();
            item.Value = "-1";
            item.Text = "全部";
            dropReason.Items.Insert(0, item);
        }
    }

    private void BindDropDuty()
    {
        List<Model.USERS> users = new BLL.USERS().GetAllList();
        if (users != null)
        {
            dropDuty.DataSource = users;
            dropDuty.DataTextField = "USERS_NAME";
            dropDuty.DataValueField = "USERS_CODE";
            dropDuty.DataBind();

            
            ListItem item = new ListItem();
            item.Text = "全部";
            item.Value = "-1";

            dropDuty.Items.Insert(0, item);
            
        }
    }

    private void Bind(bool isExport)
    {
        string sqlstr = " ";
        sqlstr += TextBox1.Text.Trim() == "" ? "" : " and RD_ID like '%" + TextBox1.Text.Trim() + "%' ";
        sqlstr += TextBox2.Text.Trim() == "" ? "" : " and RD_CUSTOMERCODE like '%" + TextBox2.Text.Trim() + "%' ";
        sqlstr += TextBox3.Text.Trim() == "" ? " and (RD_CREATEDATE>'1900-01-01' " : " and (RD_CREATEDATE>='" + TextBox3.Text.Trim() + "' ";
        sqlstr += TextBox4.Text.Trim() == "" ? " and RD_CREATEDATE<'2099-01-01') " : " and RD_CREATEDATE<='" + DateTime.Parse(TextBox4.Text.Trim()).AddDays(1).ToShortDateString() + "') ";
        
        sqlstr += TextBox6.Text.Trim() == "" ? "" : " and RD_CHECKDATE>='" + TextBox6.Text.Trim() + "' ";
        sqlstr += TextBox7.Text.Trim() == "" ? "" : " and RD_CHECKDATE<='" + DateTime.Parse(TextBox7.Text.Trim()).AddDays(1).ToShortDateString() + "' ";
        sqlstr += TextBox8.Text.Trim() == "" ? "" : " and RD_ORDER_ACHECKDATE>='" + TextBox8.Text.Trim() + "' ";
        sqlstr += TextBox9.Text.Trim() == "" ? "" : " and RD_ORDER_ACHECKDATE<='" + DateTime.Parse(TextBox9.Text.Trim()).AddDays(1).ToShortDateString() + "' ";
       
        sqlstr += dropDuty.SelectedValue == "-1" ? "" : " and RD_DUTY='" + dropDuty.SelectedValue + "' ";
        sqlstr += dropReason.SelectedValue == "-1" ? "" : " and RD_REASON='" + dropReason.SelectedValue + "' ";
        sqlstr += DropDownList1.SelectedValue == "-1" ? "" : " and ORDER_OFCODE='" + DropDownList1.SelectedValue + "' ";

        sqlstr += TextBox5.Text.Trim() == "" ? "" : " and RD_DOCID in ('" + TextBox5.Text.Trim() + "') ";

        sqlstr += CheckBox1.SelectedValue == "-1" ? "" : " and RD_CHECK=" + Int32.Parse(CheckBox1.SelectedValue) + " ";
        sqlstr += CheckBox2.SelectedValue == "-1" ? "" : " and RD_CLEAR=" + Int32.Parse(CheckBox2.SelectedValue) + " ";
        sqlstr += CheckBox3.SelectedValue == "-1" ? "" : " and RD_DELETE=" + Int32.Parse(CheckBox3.SelectedValue) + " ";

        int pageCount, rowCount;
        DataSet properties = new BLL.RETURNDOC().GetListMULU("RETURNDOC LEFT JOIN ORDERDOC ON ORDER_ID=RD_DOCID LEFT JOIN V_CUSTOMER ON RD_CUSTOMERCODE=V_CUSTOMER.DM", "RETURNDOC.*,GKMC", "RD_ID", false, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "RD_CREATEDATE", true, sqlstr, out pageCount, out rowCount);
        
        if (isExport)  //如果是导出

        {
            properties = new BLL.RETURNDOC().GetListMULU("RETURNDOC LEFT JOIN ORDERDOC ON ORDER_ID=RD_DOCID LEFT JOIN V_CUSTOMER ON RD_CUSTOMERCODE=V_CUSTOMER.DM", "RETURNDOC.*,GKMC", "RD_ID", false, rowCount, AspNetPager1.CurrentPageIndex, "RD_CREATEDATE", true, sqlstr, out pageCount, out rowCount);
            DataTable2Excel(properties.Tables[0]);
        }  

        RETURNDOCListControl1.Bind(properties);
        AspNetPager1.RecordCount = rowCount;
         
    }

    /// <summary>
    /// DataTable导出Excel
    /// </summary>
    /// <param name="dtData"></param>
    public static void DataTable2Excel(System.Data.DataTable dtData)
    {
        System.Web.UI.WebControls.DataGrid dgExport = null;
        System.Web.HttpContext curContext = System.Web.HttpContext.Current;
        System.IO.StringWriter strWriter = null;
        System.Web.UI.HtmlTextWriter htmlWriter = null;

        if (dtData != null)
        {
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
            curContext.Response.Charset = "";

            strWriter = new System.IO.StringWriter();
            htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

            dgExport = new System.Web.UI.WebControls.DataGrid();

            dtData.Columns.Add("审核");
            dtData.Columns.Add("结算");
            dtData.Columns.Add("作废");


            dtData.Columns["RD_ID"].ColumnName = "退货单号";
            dtData.Columns["RD_CUSTOMERCODE"].ColumnName = "用户帐号";
            dtData.Columns["RD_DOCID"].ColumnName = "关联订单号";
            ////dtData.Columns["发货/入库"].DataType = Type.GetType("System.bool");  
            dtData.Columns["RD_RETURNER"].ColumnName = "退货人";
            ////dtData.Columns["财务审核"].DataType = Type.GetType("System.bool"); 
            ////dtData.Columns["Acheck"].ColumnName = "结算";
            ////dtData.Columns["结算"].DataType = Type.GetType("System.bool"); 

            dtData.Columns["RD_QTY"].ColumnName = "数量";
            dtData.Columns["RD_AAMT"].ColumnName = "总金额";
            dtData.Columns["RD_CREATEDATE"].ColumnName = "制单日期";
            dtData.Columns["RD_RETURNADDRESS"].ColumnName = "退货人地址";
            dtData.Columns["GKMC"].ColumnName = "用户真实姓名";
            dtData.Columns["RD_RETURNERPHONE2"].ColumnName = "手机";
            dtData.Columns["RD_RETURNPHONE1"].ColumnName = "电话";
            dtData.Columns["RD_RETURNPOSTCODE"].ColumnName = "邮编";
            dtData.Columns["RD_NOTE"].ColumnName = "备注";
            dtData.Columns["RD_TRANCOST"].ColumnName = "物流费用";

            for (int i = 0; i < dtData.Rows.Count; i++)
            {
                dtData.Rows[i]["作废"] = dtData.Rows[i]["RD_DELETE"].ToString() == "1" ? "已作废" : "未作废";
                dtData.Rows[i]["审核"] = dtData.Rows[i]["RD_CHECK"].ToString() == "1" ? "已审核" : "未审核";
                dtData.Rows[i]["结算"] = dtData.Rows[i]["RD_CLEAR"].ToString() == "1" ? "已结算" : "未结算";
            }
            ////移除列

            dtData.Columns.Remove(dtData.Columns["RD_STATETYPECODE"]);
            dtData.Columns.Remove(dtData.Columns["RD_CREATER"]);
            dtData.Columns.Remove(dtData.Columns["RD_TAMT"]);
            dtData.Columns.Remove(dtData.Columns["RD_OFFERAMT"]);
            dtData.Columns.Remove(dtData.Columns["RD_AMT"]);
            dtData.Columns.Remove(dtData.Columns["RD_DISC"]);
            dtData.Columns.Remove(dtData.Columns["RD_TRANER"]);
            dtData.Columns.Remove(dtData.Columns["RD_COSTTYPE"]);
            dtData.Columns.Remove(dtData.Columns["RD_TRANID"]);
            dtData.Columns.Remove(dtData.Columns["RD_DELETE"]);
            dtData.Columns.Remove(dtData.Columns["RD_ACHECK"]);
            dtData.Columns.Remove(dtData.Columns["RD_CLEAR"]);



            dtData.Columns.Remove(dtData.Columns["RD_ORDER_ACHECKDATE"]);
            dtData.Columns.Remove(dtData.Columns["RD_ACHECKER"]);
            dtData.Columns.Remove(dtData.Columns["RD_DUTY"]);
            dtData.Columns.Remove(dtData.Columns["RD_REASON"]);

            //dtData.Columns.Remove(dtData.Columns["RD_SECDEAL"]);
            //dtData.Columns.Remove(dtData.Columns["RD_SUBITG"]);
            //dtData.Columns.Remove(dtData.Columns["RD_RETURNTYPE"]);

            dtData.Columns.Remove(dtData.Columns["RD_CHECK"]);
            dtData.Columns.Remove(dtData.Columns["RD_CHECKER"]);

            dtData.Columns.Remove(dtData.Columns["RD_CHECKDATE"]);
            dtData.Columns.Remove(dtData.Columns["RD_DELETER"]);
            dtData.Columns.Remove(dtData.Columns["RD_DELETEDATE"]);
            dtData.Columns.Remove(dtData.Columns["RD_CLEAREDAMT"]);
            dtData.Columns.Remove(dtData.Columns["RD_FIELD1"]);
            dtData.Columns.Remove(dtData.Columns["RD_FIELD2"]);

            dtData.Columns.Remove(dtData.Columns["RD_ITGBUY"]);
            dtData.Columns.Remove(dtData.Columns["RD_SUBITG"]);
            dtData.Columns.Remove(dtData.Columns["RD_RETURNTYPE"]);
            dtData.Columns.Remove(dtData.Columns["RD_SECDEAL"]);

            dgExport.DataSource = dtData.DefaultView;
            dgExport.AllowPaging = false;
            dgExport.DataBind();

            dgExport.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }
    } 

    protected void Button1_Click(object sender, EventArgs e)
    {
        Bind(false);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind(false);
        //int pageCount, rowCount;
        //List<Model.ORDERDOC> properties = new BLL.ORDERDOC().GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "ORDER_ID", false, "", out pageCount, out rowCount);
        //ORDERDOCListControl1.Bind(colors);
    }
    public void Add()
    {
        if (Check.CheckPermission("400101", "00001"))
        {
            Response.Redirect("NewReturnDoc.aspx");
        }
        else
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
    }
    public void Update()
    {
        if (Check.CheckPermission("400101", "00002"))
        {
            List<string> s = RETURNDOCListControl1.Selected;
            if (s.Count > 0)
            {
                Response.Redirect("ReturnDocEdit.aspx?code=" + s[0]);
            }
            else
            {
                Share.Alert(this.Page, "请选择一条需要编辑的记录"); return;
            }
        }
        else
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
    }
    /// <summary>
    /// by winner add 2008-04-20  打印
    /// </summary>
    public void Print()
    {
        //if (Check.CheckPermission("400101", "00002"))
        //{
            List<string> s = RETURNDOCListControl1.Selected;
            if (s.Count > 0)
            {
                string openurlstr = "";
                string sprinttype = "0";//默认打印时不显示界面
                if (ConfigurationManager.AppSettings["PrintType"] != null && ConfigurationManager.AppSettings["PrintType"] != "") sprinttype = ConfigurationManager.AppSettings["PrintType"];
                //By winner add打印
                for (int i = 0; i < s.Count; i++)
                {
                    openurlstr += "window.open('../BillReport.aspx?DJBH=" + s[i] + "&ReportModule=DDGL/SDTHD&ReportName=SDTHD&PrintType=" + sprinttype + "');";
                }
                Page.RegisterStartupScript("StartUp", "<script language=javascript>" + openurlstr + "</script>"); 

                //Response.Redirect("ReturnDocEdit.aspx?code=" + s[0]);
            }
            else
            {
                Share.Alert(this.Page, "请选择要打印的退单"); return;
            }
        //}
        //else
        //{
        //    Share.Alert(this.Page, "无权操作"); return;
        //}
    }

    /// <summary>
    /// by winner add 2008-04-20  导出
    /// </summary>
    public void Out()
    {
        Bind(true);
        //Share.Alert(this.Page, "功能完善中..."); return; 
    }

    protected void BUTTONDJLIST1_Load(object sender, EventArgs e)
    {

    }
}
